﻿Imports System.Data
Imports MyGeneration.dOOdads

Public Class rc_Proveedores

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property


#Region "Proveedores"

    Public Function Insert_Proveedores(conn As String, nombre As String, direccion As String, telefono As String, celular As String,
                                    codCiudad As Integer, ruc As String, codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim pro As New Class_RC_Data.Class_RC.PROVEEDORES(conn)

            pro.AddNew()
            pro.Nombre = nombre
            pro.Direccion = direccion
            pro.Telefono = telefono
            pro.Celular = celular
            pro.Codciudad = codCiudad
            pro.Ruc = ruc
            pro.CodUsuario = codUsuario
            pro.FechaActualizacion = DateTime.Now
            pro.Save()

            tx.CommitTransaction()

            Return pro.CodProveedor

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Proveedores(conn As String, codProveedor As Integer, nombre As String, direccion As String, telefono As String, celular As String,
                                    codCiudad As Integer, ruc As String, codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim pro As New Class_RC_Data.Class_RC.PROVEEDORES(conn)
            pro.Where.CodProveedor.Value = codProveedor

            If pro.Query.Load Then

                pro.Nombre = nombre
                pro.Direccion = direccion
                pro.Telefono = telefono
                pro.Celular = celular
                pro.CodCiudad = codCiudad
                pro.Ruc = ruc
                pro.CodUsuario = codUsuario
                pro.FechaActualizacion = DateTime.Now
                pro.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Proveedores(conn As String, codProveedor As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim pro As New Class_RC_Data.Class_RC.PROVEEDORES(conn)
            pro.Where.CodProveedor.Value = codProveedor

            If pro.Query.Load Then

                pro.MarkAsDeleted()
                pro.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Proveedores_All(conn As String, codProveedor As Integer, nombre As String, ruc As String, codCiudad As Integer) As DataTable
        Try

            Dim vPro As New Class_RC_Data.Class_RC.VistaProveedores(conn)

            If codProveedor > 0 Then
                vPro.Where.CodProveedor.Value = codProveedor
            End If

            If nombre <> "" Then
                vPro.Where.Nombre.Value = "%" & nombre & "%"
                vPro.Where.Nombre.Operator = WhereParameter.Operand.Like
            End If

            If codCiudad > 0 Then
                vPro.Where.CodCiudad.Value = codCiudad
            End If

            vPro.Query.AddOrderBy("nombre", WhereParameter.Dir.ASC)

            If vPro.Query.Load Then
                If vPro.DefaultView.Table.Rows.Count > 0 Then
                    Return vPro.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Ciudades(conn As String) As DataTable
        Try

            Dim vTipoCiudad As New Class_RC_Data.Class_RC.CIUDAD(conn)

            If vTipoCiudad.Query.Load Then
                If vTipoCiudad.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipoCiudad.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

End Class
